<template>
  <div>
    <van-nav-bar title="登录" />

    <van-form @submit="onSubmit">
      <van-field
        required
        v-model="mobile"
        label="手机号"
        placeholder="手机号"
        :rules="rules.mobile"
      />
      <van-field
        :rules="rules.code"
        required
        v-model="code"
        label="验证码"
        placeholder="验证码"
      />
      <div style="margin: 16px">
        <van-button
          :disabled="isLoad"
          :loading="isLoad"
          loading-text="登陆中..."
          round
          block
          type="info"
          native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import axios from '@/utils/request'
export default {
  name: 'Login',
  data() {
    return {
      isLoad: false, // 代表 是否在登录中
      mobile: '13637268276',
      code: '246810',
      rules: {
        mobile: [
          {
            required: true,
            message: '手机号不能为空'
          },
          // 正则校验
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确'
          }
        ],
        code: [
          {
            required: true,
            message: '手机号不能为空'
          },
          // 正则校验
          {
            pattern: /^\d{6}$/,
            message: '验证码格式不正确'
          }
        ]
      }
    }
  },
  methods: {
    async onSubmit() {
      this.isLoad = true
      try {
        await axios({
          method: 'POST',
          url: '/v1_0/authorizations',
          data: {
            mobile: this.mobile,
            code: this.code
          }
        })
        // alert('登陆成功' + res.message)
        // 弹框提示
        this.$toast.success('登陆成功')
        // 跳转页面
        this.$router.push('/layout')
      } catch (e) {
        this.$toast.fail(e.message)
      }
      this.isLoad = false
    }
  }
}
</script>

<style lang="less" scoped></style>
